package freighttmpl

import (
	"context"
	"strings"

	"mall/common/modelx"
	"mall/service/backend/common/utils/pointy"
	"mall/service/setting/model"
	"mall/service/setting/rpc/internal/svc"
	"mall/service/setting/rpc/internal/utils/errorhandler"
	"mall/service/setting/rpc/types/setting"

	"github.com/zeromicro/go-zero/core/logx"
)

type BackendGetFreightTemplateListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewBackendGetFreightTemplateListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *BackendGetFreightTemplateListLogic {
	return &BackendGetFreightTemplateListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// group: freighttmpl
func (l *BackendGetFreightTemplateListLogic) BackendGetFreightTemplateList(in *setting.FreightTemplateListReq) (*setting.FreightTemplateListResp, error) {
	var orderBys []string
	if len(in.OrderBy) > 0 {
		for _, orderBy := range in.OrderBy {
			var order string = " ASC"
			if orderBy.Order == "1" {
				order = " DESC"
			}
			if orderBy.Field == "id" {
				orderBys = append(orderBys, model.SysFreightTemplateColumns.ID + order)
			}
		}
	}

	var stati string
	if len(in.FilterBy) > 0 {
		for _, filterBy := range in.FilterBy {
			if filterBy.Field == "status" {
				stati = filterBy.In
			}
		}
	}

	var page uint64
	var pageSize uint64
	var restricted bool
	filterMap := make(map[string]map[string][]any)
	if in.Ids != nil {
		if vals := modelx.UniqueInterfaces(*in.Ids, 64); len(vals) > 0 {
			filterMap[model.SysFreightTemplateColumns.ID] = map[string][]any{
				"in": vals,
			}
			restricted = true
		}
	}
	if !restricted {
		page = in.Page
		pageSize = in.PageSize
	}

	statusIn := modelx.UniqueInterfaces(stati, 32)
	statusMap := make(map[string][]any)
	if len(statusIn) > 0 {
		statusMap["in"] = statusIn
	}
	if in.Status != nil && *in.Status > 0 {
		statusMap["equalTo"] = []any{*in.Status - 1}
	}
	filterMap[model.SysFreightTemplateColumns.Status] = statusMap

	if in.Name != nil {
		filterMap[model.SysFreightTemplateColumns.Name] = map[string][]any{
			"contain": []any{*in.Name},
		}
	}
	if in.FreeShipping != nil {
		filterMap[model.SysFreightTemplateColumns.FreeShipping] = map[string][]any{
			"equalTo": []any{*in.FreeShipping},
		}
	}
	if in.ChargeMode != nil {
		filterMap[model.SysFreightTemplateColumns.ChargeMode] = map[string][]any{
			"equalTo": []any{*in.ChargeMode},
		}
	}

	var orderBy string
	if len(orderBys) > 0 {
		orderBy = strings.Join(orderBys, ",")
	} else {
		orderBy = model.SysFreightTemplateColumns.Sort + " DESC"
	}
	res, count, err := l.svcCtx.FreightTemplate.GetList(l.ctx, model.M{
		"page":     page,
		"pageSize": pageSize,
		"orderBy":  orderBy,
	}, 1, filterMap)
	if err != nil {
		return nil, errorhandler.DefaultRepositoryError(l.Logger, err, in)
	}

	resp := &setting.FreightTemplateListResp{
		Total: uint64(count),
	}
	for _, v := range res {
		resp.List = append(resp.List, &setting.FreightTemplateInfo{
			Id:           &v.ID,
			Name:         &v.Name,
			Remark:       &v.Remark,
			FreeShipping: pointy.GetPointer(uint32(v.FreeShipping)),
			ChargeMode:   pointy.GetPointer(uint32(v.ChargeMode)),
			Sort:         pointy.GetPointer(uint32(v.Sort)),
			Status:       pointy.GetPointer(uint32(v.Status)),
			CreatedAt:    pointy.GetPointer(modelx.FormatTime(v.CreatedAt, "")),
			UpdatedAt:    pointy.GetPointer(modelx.FormatTime(v.UpdatedAt, "")),
			DeletedAt:    pointy.GetPointer(modelx.FormatNullDotTime(v.DeletedAt, "")),
		})
	}

	return resp, nil
}
